//
//  WSGAllCollectionView.m
//  oneDollar
//
//  Created by Laki on 16/6/17.
//  Copyright © 2016年 com.Laki.corder. All rights reserved.
//

#import "WSGAllCollectionView.h"

#import "WSGAllCell.h"

@interface WSGAllCollectionView()<UICollectionViewDataSource,UICollectionViewDelegate>
/** 全局属性*/
@property (nonatomic, weak) UICollectionView *collectionView;
@end

@implementation WSGAllCollectionView
/** 重用标识符*/
static NSString * const ID = @"WSGAllCell";

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        // 创建流水不惧
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

        // 创建UICollectionView
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        
        // 设置数据源
        collectionView.dataSource = self;
        collectionView.delegate = self;
        
        // 隐藏纵向指示条
        collectionView.showsVerticalScrollIndicator = NO;
        
        // 禁止CollectionView滚动
        collectionView.scrollEnabled = NO;

        // 保存属性
        self.collectionView = collectionView;
        
        // 设置背景颜色间隔就能看到灰色分割线
        collectionView.backgroundColor = [UIColor colorWithRed:241.0 / 255.0 green:241.0 / 255.0 blue:241.0 / 255.0 alpha:1.0];
        
        [self addSubview:collectionView];
        
        // 注册WSGAllCell
        [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([WSGAllCell class]) bundle:nil] forCellWithReuseIdentifier:ID];
        
        // 接收通知刷新数据
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadNextData) name:WSGAllCollectionViewNotification object:WSGAllCollectionViewNotification];
    }
    return self;
}

/*
 * 移除通知
 */
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:WSGAllCollectionViewNotification object:WSGAllCollectionViewNotification];
}

/*
 * 刷新数据
 */
- (void)loadNextData{
    [self.collectionView reloadData];
}

/*
 * 布局子控件
 */
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 设置UICollectionView的Frame
    self.collectionView.frame = self.bounds;
    
    // 获取布局
    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    
    // 设置每个Cell的大小
    CGFloat itemWH = ScreenW / col - margin;
    layout.itemSize = CGSizeMake(itemWH, itemWH);
    
    // 设置每个Cell的行和列间距
    layout.minimumInteritemSpacing = margin;
    layout.minimumLineSpacing = margin;
}

#pragma mark --------------------
#pragma mark UICollectionViewDataSource
/*
 * 显示多少个Cell
 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.allItemM.count;
}

/*
 * 每个Cell显示什么数据
 */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 从缓存池中取出WSGAllCell
    WSGAllCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    // 传递模型
    cell.allItem = self.allItemM[indexPath.item];
    
    return cell;
}

@end
